package com.okbihuo.perm.component.sms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.okbihuo.perm.component.sms.domain.Sms;
import com.okbihuo.perm.component.sms.logic.type.SmsType;
import com.okbihuo.perm.component.sms.mapper.SmsMapper;
import com.okbihuo.perm.component.sms.service.SmsService;
import com.okbihuo.perm.core.mp.base.MyServiceImpl;
import com.okbihuo.perm.core.tool.utils.StringUtil;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;


/**
 * 短信接口实现层
 * @author stt
 */
@Service
public class SmsServiceImpl extends MyServiceImpl<SmsMapper, Sms> implements SmsService {

    @Override
    public Sms getEffectiveSMS(String target, String captcha, SmsType type) {
        QueryWrapper<Sms> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("target", target);
        queryWrapper.eq("captcha", captcha);
        queryWrapper.ge(StringUtil.humpToUnderline("expiresAt"),System.currentTimeMillis());
        queryWrapper.eq("avaliable", 1);
        queryWrapper.eq("type", type.toString());
        return getOne(queryWrapper);
    }

    /**
     * 获取今日某个ip地址发送的次数
     * @param ip
     * @return
     */
    @Override
    public List<Sms> listByIpWithToday(String ip) {
        QueryWrapper<Sms> queryWrapper = new QueryWrapper<>();
        LocalDateTime time = LocalDate.now().plusDays(1).atStartOfDay().minusSeconds(1);
        queryWrapper.gt(StringUtil.humpToUnderline("createdDate"),time.toInstant(ZoneOffset.of("+8")).toEpochMilli());
        return list(queryWrapper);
    }
}
